package org.dromara.oce.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.oce.domain.qo.OceMicroAppQo;
import org.dromara.oce.domain.vo.OceMicroAppVo;
import org.dromara.oce.domain.bo.OceMicroAppBo;
import org.dromara.oce.service.OceMicroAppService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 字节小程序
 *
 * @author dsbb
 * @date 2025-06-04
 * @module 管理平台接口
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oce/microApp")
public class OceMicroAppController extends BaseController {

    private final OceMicroAppService oceMicroAppService;

    /**
     * 查询字节小程序列表
     */
    @SaCheckPermission("oce:byte:list")
    @GetMapping("/list")
    public TableDataInfo<OceMicroAppVo> list(OceMicroAppQo qo, PageQuery pageQuery) {
        return oceMicroAppService.queryPageList(qo, pageQuery);
    }

    /**
     * 导出字节小程序列表
     */
    @SaCheckPermission("oce:byte:export")
    @Log(title = "字节小程序", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OceMicroAppQo qo, HttpServletResponse response) {
        List<OceMicroAppVo> list = oceMicroAppService.queryList(qo);
        ExcelUtil.exportExcel(list, "字节小程序", OceMicroAppVo.class, response);
    }

    /**
     * 获取字节小程序详细信息
     *
     * @param microAppId 主键
     */
    @SaCheckPermission("oce:byte:query")
    @GetMapping("/{microAppId}")
    public R<OceMicroAppVo> getInfo(@NotNull(message = "主键不能为空")
                                    @PathVariable Long microAppId) {
        return R.ok(oceMicroAppService.queryById(microAppId));
    }

    /**
     * 新增字节小程序
     */
    @SaCheckPermission("oce:byte:add")
    @Log(title = "字节小程序", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OceMicroAppBo bo) {
        return toAjax(oceMicroAppService.insertByBo(bo));
    }

    /**
     * 修改字节小程序
     */
    @SaCheckPermission("oce:byte:edit")
    @Log(title = "字节小程序", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OceMicroAppBo bo) {
        return toAjax(oceMicroAppService.updateByBo(bo));
    }

    /**
     * 删除字节小程序
     *
     * @param microAppIds 主键串
     */
    @SaCheckPermission("oce:byte:remove")
    @Log(title = "字节小程序", businessType = BusinessType.DELETE)
    @DeleteMapping("/{microAppIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] microAppIds) {
        return toAjax(oceMicroAppService.deleteWithValidByIds(List.of(microAppIds), true));
    }
}
